package db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import model.Customer;
import model.Enterprise;
import model.Person;
import model.type.SubjectType;

public class CustomerDAO {
	protected static final String TABLE = "customer";	
	private static String onSqlError = "SQL statement fault";
	
	private static final String 
		ID_FIELD = "customer",	
		SUBJECT_FIELD = "subject_fk",
		SUBJECT_TYPE_FIELD = "subject_type_fk";
	
	private static final String INSERT_STATEMENT_STRING = 
			"INSERT INTO "+TABLE+" ("+SUBJECT_FIELD+", "+SUBJECT_TYPE_FIELD+") VALUES (?, ?)";
	
	private static final String UPDATE_STATEMENT_STRING = 
			"UPDATE "+TABLE+" SET " +SUBJECT_FIELD+" = ?, "+SUBJECT_TYPE_FIELD+" = ? " +
			"WHERE "+ID_FIELD+" = ?";
	
	private static final String DELETE_STATEMENT_STRING =
			"DELETE FROM "+TABLE+" WHERE "+ID_FIELD+" = ?";
	
	private static final String SUBJECT_EXISTS_STATEMENT_STRING =
			"SELECT * FROM "+TABLE+" WHERE "+SUBJECT_FIELD+" = ? AND "+SUBJECT_TYPE_FIELD+" = ?";
	
	private static PreparedStatement insertStatement, updateStatement, deleteStatement, subjectExistsStatement;
	
	static{
		try {
			insertStatement = ImbiConnection.getConnection().prepareStatement(INSERT_STATEMENT_STRING);
			updateStatement = ImbiConnection.getConnection().prepareStatement(UPDATE_STATEMENT_STRING);
			deleteStatement = ImbiConnection.getConnection().prepareStatement(DELETE_STATEMENT_STRING);
			subjectExistsStatement = ImbiConnection.getConnection().prepareStatement(SUBJECT_EXISTS_STATEMENT_STRING);
		} catch (SQLException e) {						
			System.exit(0);
		}
	}
	
	public Customer find(int id) {			
		try{				
			Statement st = ImbiConnection.getConnection().createStatement();
			st.executeQuery("SELECT * FROM "+TABLE+" WHERE "+ID_FIELD+" = "+id);				
			Customer c = extractCustomer(st.getResultSet()).get(0);
			return c;
		} catch (SQLException | IndexOutOfBoundsException e){
				System.out.println(onSqlError);				
				return null;
		}		
	}
	
	public ArrayList<Customer> findByPersonId(Person p) {			
		try{				
			Statement st = ImbiConnection.getConnection().createStatement();
			st.executeQuery("SELECT * FROM "+TABLE+" WHERE "+SUBJECT_FIELD+" = "+p.getId()+
					" AND "+SUBJECT_TYPE_FIELD+" = "+SubjectType.PERSON);				
			return extractCustomer(st.getResultSet());			
		} catch (SQLException | IndexOutOfBoundsException e){
				System.out.println(onSqlError);				
				return null;
		}		
	}
	
	public ArrayList<Customer> findByEnterpriseId(Enterprise ent) {			
		try{				
			Statement st = ImbiConnection.getConnection().createStatement();
			st.executeQuery("SELECT * FROM "+TABLE+" WHERE "+SUBJECT_FIELD+" = "+ent.getId()+
					" AND "+SUBJECT_TYPE_FIELD+" = "+SubjectType.ENTERPRISE);				
			return extractCustomer(st.getResultSet());			
		} catch (SQLException | IndexOutOfBoundsException e){
				System.out.println(onSqlError);				
				return null;
		}		
	}

	private ArrayList<Customer> extractCustomer(ResultSet r) throws SQLException {
		ArrayList<Customer> list = new ArrayList<Customer>();
		while(r.next()){
			Customer c = new Customer(
				r.getInt(1),
				r.getInt(2),
				r.getInt(3)
			);
			list.add(c);
		}
		return list;
	}		
	

	public void insert(Customer c){
		if(subjectExists(c.getSubject(),c.getSubjectType())) return;
		try{											
		    insertStatement.setInt(1, c.getSubject());
		    insertStatement.setInt(2, c.getSubjectType());			    	    
		    insertStatement.executeUpdate();			
		} catch (SQLException s){
				System.out.println(onSqlError);									
		}		
	}
	
	public void update(Customer c){
		try{										
		    updateStatement.setInt(1, c.getSubject());
		    updateStatement.setInt(2, c.getSubjectType());
		    updateStatement.setInt(3, c.getId());
		    updateStatement.executeUpdate();			
		} catch (SQLException s){
				System.out.println(onSqlError);									
		}			
	}
	
	public void delete(Customer c){
		try{										
		    deleteStatement.setInt(1, c.getId());		    
		    deleteStatement.executeUpdate();			
		} catch (SQLException s){
				System.out.println(onSqlError);						
		}			
	}	
	
	public boolean subjectExists(int subjectId, int type){
		try{										
		    subjectExistsStatement.setInt(1, subjectId);	
		    subjectExistsStatement.setInt(2, type);		
		    subjectExistsStatement.execute();		
		    ResultSet r = subjectExistsStatement.getResultSet();
		    return r.next();
		} catch (SQLException s){
				System.out.println(onSqlError);				
				return false;
		}			
	}	
}
